package com.lzb.yabbs.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gitonway.lee.niftynotification.lib.Effects;
import com.gitonway.lee.niftynotification.lib.NiftyNotificationView;
import com.lzb.yabbs.AppConfig;
import com.lzb.yabbs.R;
import com.lzb.yabbs.net.OkHttpClientRequest;
import com.lzb.yabbs.util.PersonalData;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuzaibing on 2015/9/17.
 */
public class SendMessageDialog {
    private String TAG=SendMessageDialog.class.getSimpleName();

    private Context mContext;
    private String recviceUser;
    private int recviceId;

    private final int SEND_MESSAGE_SUCCESS=0x197;
    private final int SEND_MESSAGE_FAIL=0x812;

    public SendMessageDialog(Context mContext,String recviceUser,int recviceId){
        this.mContext=mContext;
        this.recviceUser=recviceUser;
        this.recviceId=recviceId;
    }

    private MyHandler mHandler=new MyHandler(mContext);

    class MyHandler extends Handler{
        WeakReference<Activity> weakReference;

        public MyHandler(Context context){
            weakReference=new WeakReference<>((Activity)context);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case SEND_MESSAGE_SUCCESS:
                    NiftyNotificationView.build((Activity)mContext, mContext.getString(R.string.send_message_success_str), Effects.flip, R.id.mll)
                            .setIcon(R.mipmap.ic_launcher).    //You must call this method if you use ThumbSlider effect
                            show();
                    break;
                case SEND_MESSAGE_FAIL:
                    NiftyNotificationView.build((Activity)mContext, mContext.getString(R.string.send_message_fail_str), Effects.flip, R.id.mll)
                            .setIcon(R.mipmap.ic_launcher).    //You must call this method if you use ThumbSlider effect
                            show();
                    break;
            }
        }
    };

    public void show(){
        View view= LayoutInflater.from(mContext).inflate(R.layout.send_msg_dialog_layout,null);
        TextInputLayout titleTil= (TextInputLayout) view.findViewById(R.id.title_et);
        TextInputLayout contentTil= (TextInputLayout) view.findViewById(R.id.content_et);
        titleTil.setHint(mContext.getString(R.string.title));
        contentTil.setHint(mContext.getString(R.string.content_string));
        final EditText titleEt=titleTil.getEditText();
        final EditText contentEt=contentTil.getEditText();
        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
        builder.setTitle(mContext.getString(R.string.send_msg_to_string) + recviceUser);
        builder.setView(view);
        final ProgressDialog progressDialog=new ProgressDialog(mContext);
        progressDialog.setTitle("发送中...");


        builder.setNegativeButton(mContext.getString(R.string.cancle_string), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton(mContext.getString(R.string.sure_string), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, int which) {

                String titleStr = titleEt.getText().toString();
                String contentStr = contentEt.getText().toString();

                if (titleStr == null || contentStr == null)
                    return;
                progressDialog.show();
                List<BasicNameValuePair> pairs = new ArrayList<>();
                BasicNameValuePair pair = new BasicNameValuePair("uid", PersonalData.getUserId(mContext) + "");
                BasicNameValuePair pair1 = new BasicNameValuePair("token", PersonalData.getToken(mContext));
                BasicNameValuePair pair2 = new BasicNameValuePair("send_to", recviceId + "");
                BasicNameValuePair pair3 = new BasicNameValuePair("title", titleStr);
                BasicNameValuePair pair4 = new BasicNameValuePair("content", contentStr);

                pairs.add(pair);
                pairs.add(pair1);
                pairs.add(pair2);
                pairs.add(pair3);
                pairs.add(pair4);

                OkHttpClientRequest.requestGet(AppConfig.URL_SEND_MESSAGE, pairs, new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Log.d(TAG, "onFailure");
                        progressDialog.dismiss();
                        dialog.dismiss();
                        mHandler.sendEmptyMessage(SEND_MESSAGE_FAIL);
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {

                        String resultStr = response.body().string();
                        Log.d(TAG, resultStr);
                        JSONObject object = JSON.parseObject(resultStr);
                        Log.d(TAG, object.toString());
                        if (object.getInteger("code") != 0) {
                            mHandler.sendEmptyMessage(SEND_MESSAGE_SUCCESS);
                            progressDialog.dismiss();
                            dialog.dismiss();
                        } else {
                            mHandler.sendEmptyMessage(SEND_MESSAGE_FAIL);
                            progressDialog.dismiss();
                        }

                    }
                });
            }
        });

        builder.create().show();
    }

}
